package com.suse.ecommerce.serviceImp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suse.ecommerce.dao.ProductMapper;
import com.suse.ecommerce.pojo.Product;
import com.suse.ecommerce.pojo.ProductExample;
import com.suse.ecommerce.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 * @auth zst
 * @Time 2019-7-6 8:56
 * */
@Service
public class ProductServiceImp implements ProductService {

    private final
    ProductMapper productMapper;

    @Autowired
    public ProductServiceImp(ProductMapper productMapper) {
        this.productMapper = productMapper;
    }

    /*
     * 根据产品记录集合添加商品
     * @auth zst
     * @param product 产品记录集合
     * */
    @Override
    public void addProduct(Product product) {
        productMapper.insert(product);
    }

    @Override
    public void delProduct(Product product) {
        productMapper.deleteByPrimaryKey(product.getId());
    }

    @Override
    public void updateProduct(Product product) {
        productMapper.updateByPrimaryKeySelective(product);
    }

    /**
     * 查询商品
     */
    @Override
    public PageInfo<Product> selectProducts(Product product, int pn,Integer status) {
        PageHelper.startPage(pn,6);
        ProductExample productExample = new ProductExample();
        ProductExample.Criteria circle = productExample.createCriteria();
        if(product.getCategoryId() != null) {
            circle.andCategoryIdEqualTo(product.getCategoryId());
        }
        if(status != null){
            product.setStatus(1);
        }
        List<Product> products = productMapper.getProducts(product);
        PageInfo<Product> productPageInfo = new PageInfo<>(products,6);
        return productPageInfo;
    }

    @Override
    public Product queryProductById(long productId) {
        Product product = productMapper.selectByPrimaryKey(productId);
        return product;
    }

    @Override
    public List<Product> setProductStatus(Product product) {
        Product product1 = productMapper.selectByPrimaryKey(product.getId());
        if(product1.getId() == null) {
            product1 = null;
        }
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andIdEqualTo(product.getId());
        Product productStatus = product1;
        productStatus.setStatus(product.getStatus());
        productMapper.updateByExampleWithBLOBs(productStatus,productExample);
        List<Product>products = productMapper.getProducts(product1);
        return products;
    }
}
